import { AddPrinterComponent } from './../addPrinter/addPrinter.component';
import { NavController, NavParams } from 'ionic-angular';
import { DeviceManager } from './../../../../common/mpj-deviceCenter/src/deviceManager';
import { MessageBoxService, GrowlMessageType } from './../../../../component/messageBoxService';
import { ConfirmParams } from './../../../../component/primeng/confirm/confirm.module';
import { MdDialog, MdSnackBar } from '@angular/material';
import { PrintComponentService } from './../templates/printcontainer.component';
import { ActivatedRoute } from '@angular/router';
import { Component, OnDestroy } from '@angular/core';
import { NavigateService } from "../../../../service/navigate.service";
import { MessageResult } from "../../../../component/index";

@Component({
    selector: 'printer-detail',
    templateUrl: './printerDetail.component.html',
    styleUrls: ['./printerDetail.component.scss']
})

export class PrinterDetailComponent implements OnDestroy {
    constructor(
        private router: NavigateService,
        private acRouter: ActivatedRoute,
        private deviceManager: DeviceManager,
        private dialog: MdDialog,
        private msgSvr: MessageBoxService,
        public snackBar: MdSnackBar,
        public navCtrl: NavController,
        private navParams: NavParams
    ) { }
    private sub;
    printerManager: any = {};
    currentPrinter: any = {};
    currentInstructionSet;
    currentPageSize;
    currentScrollRow;
    currentAutoCut;
    ionViewWillEnter() {
        this.router.registGoBack(() => {
            let backBtn = document.getElementById("printerdetail-back-btn");
            if (backBtn) {
                backBtn.click();
            }
        });
        this.printerManager = this.deviceManager.getManagerByType("printerManager");
        // this.sub = this.acRouter.params.subscribe((params) => {
        let printerId = this.navParams.get("printerId");
        let printer = this.printerManager.getDeviceById(printerId);
        this.currentPrinter = printer;
        this.initPrinterParams()
        // })
    }

    /**
     * 初始打印机的参数
     */
    initPrinterParams() {
        this.currentInstructionSet = this.printerManager.instructionSets.find((ins) => {
            return ins.value == this.currentPrinter.instructionSet;
        });
        this.currentPageSize = this.printerManager.selectPageSizes.find((pSize) => {
            return pSize.value == this.currentPrinter.paperSize;
        });
        this.currentScrollRow = this.printerManager.paperScrollRows.find((pRow) => {
            return pRow.value == this.currentPrinter.paperScrollRows;
        });
        this.currentAutoCut = this.currentPrinter.autoCut;
    }

    /**
     * 根据选择设置打印的参数
     */
    setPrinterParams() {
        this.currentPrinter.instructionSet = this.currentInstructionSet.value;
        this.currentPrinter.paperSize = this.currentPageSize.value;
        this.currentPrinter.paperScrollRows = this.currentScrollRow.value;
        this.currentPrinter.autoCut = this.currentAutoCut;
    }
    /**
     * 打印测试
     */
    async testPrint() {
        this.setPrinterParams();
        let printParams = {
            token: this.router.Token,
            store: this.router.CurrentStore,
            isTestPrint: true,
            printers: [this.currentPrinter]
        }
        let printTask: any = await PrintComponentService.current.show(printParams);
        let result = await this.printerManager.printHtml(printTask);
        let msg = this.printerManager.errMsg(result.data);
        if (msg) {
            this.msgSvr.Growl(GrowlMessageType.Warning, this.snackBar, msg);
        }
    }
    /**
     * 删除打印机
     */
    async deletePrinter() {
        let confirmParams: ConfirmParams = {
            content: "确认删除该打印机？"
        }
        let msg = await MessageBoxService.Current.Show(this.dialog, confirmParams);
        if (msg == MessageResult.Ok) {
            let result = this.printerManager.deleteDeviceByID(this.currentPrinter.id);
            this.msgSvr.Growl(GrowlMessageType.Warning, this.snackBar, "删除成功");
            if (result.status == 200) {
                // this.router.navigate(["printer"]);
                this.navCtrl.pop({ animate: false });
            }
        }
    }

    /**
     * 更新打印机
     */
    async updatePrinter() {
        let confirmParams: ConfirmParams = {
            content: "确认保存设置？"
        }
        let msg = await MessageBoxService.Current.Show(this.dialog, confirmParams);
        if (msg == MessageResult.Ok) {
            this.setPrinterParams();
            let result = this.printerManager.updateDeviceById(this.currentPrinter);
            if (result) {
                this.msgSvr.Growl(GrowlMessageType.Warning, this.snackBar, "修改成功");
                // this.router.navigate(["printer"]);
                this.navCtrl.pop({ animate: false });
            } else {
                this.msgSvr.Growl(GrowlMessageType.Warning, this.snackBar, "修改失败");
            }
        }
    }

    ngOnDestroy() {
        if (this.sub) {
            this.sub.unsubscribe();
        }
    }
    goBack() {
        // return this.router.navigate(["printer"]);
        this.navCtrl.pop({ animate: false });
    }

    addPrinter() {
        // this.router.navigate(['addPrinter']);
        this.navCtrl.push(AddPrinterComponent, {}, { animate: false });
    }

}